package com.smalljj.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smalljj.bean.Friend;
import com.smalljj.bean.Message;
import com.smalljj.bean.User;
import com.smalljj.bean.UserKey;
import com.smalljj.service.FriendService;
import com.smalljj.service.MessageService;
import com.smalljj.service.UserService;
import com.smalljj.util.SmalljjUtil;

@Controller
@RequestMapping("friend")
public class FriendController {

	@Autowired
	private UserService userService;

	@Autowired
	private FriendService friendService;
	
	@Autowired
	private MessageService messageService;
	
	@RequestMapping( value="/myfriend",method=RequestMethod.POST )
    @ResponseBody
	public List<Map<String,Object>> myfriend(HttpServletRequest request) throws Exception {
		String userIdBase64 = request.getParameter("userId");
		String userId = SmalljjUtil.decodeBase64(userIdBase64);
		List<Map<String,Object>> friendList = friendService.getFriendList(userId);
		return friendList;
	}

	@RequestMapping( value="/addfriend",method=RequestMethod.POST )
	@ResponseBody
	public Map<String,Object> addfriend(HttpServletRequest request) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		String auserBase64 = request.getParameter("auser");
		String busernameBase64 = request.getParameter("busername");
		String auser = SmalljjUtil.decodeBase64(auserBase64);
		String busername = SmalljjUtil.decodeBase64(busernameBase64);
		
		User user = userService.findUserByName(busername);
		if(user==null){//用户不存在
			jsonMap.put("result", false);
			jsonMap.put("message", "该用户不存在!");
			return jsonMap;
		}
		if(user.getId().equals(auser)){//添加自己
			jsonMap.put("result", false);
			jsonMap.put("message", "不能添加自己为好友!");
			return jsonMap;
		}
		Friend existFriend = friendService.getFriend(auser,user.getId().toString());
		if(existFriend!=null){
			jsonMap.put("result", false);
			jsonMap.put("message", "已经是好友，不需要重复添加!");
			return jsonMap;
		}
		Friend newFriend =  new Friend();
		newFriend.setAuser(new BigDecimal(auser));
		newFriend.setBuser(new BigDecimal(user.getId().toString()));
		newFriend.setFriendname(user.getUsername());
		boolean result = friendService.insert( newFriend );
		if(result){
			Message message = new Message();
			message.setAdel(new BigDecimal("1"));
			message.setBuser(new BigDecimal(user.getId().toString()));
			message.setBdel(new BigDecimal("0"));
			message.setMsg(userService.findByKey(new UserKey(new BigDecimal(auser))).getUsername() +"将您添加为好友!");
			message.setSendtime(SmalljjUtil.getCurrentFormatStr("yyyy-MM-dd HH:mm:ss"));
			messageService.insert(message);
			
			jsonMap.put("result", true);
			jsonMap.put("message", "好友添加成功!");
			return jsonMap;
		}else{
			jsonMap.put("result", false);
			jsonMap.put("message", "好友添加失败!");
			return jsonMap;
		}
	}
	
	@RequestMapping( value="/delfriend",method=RequestMethod.POST )
	@ResponseBody
	public Map<String,Object> delfriend(HttpServletRequest request) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		String friendIdBase64 = request.getParameter("friendId");
		String friendId = SmalljjUtil.decodeBase64(friendIdBase64);
		Friend friend = new Friend();
		friend.setId(new BigDecimal(friendId));
		boolean result = friendService.delete( friend );
		if(result){
			jsonMap.put("result", true);
			jsonMap.put("message", "好友删除成功!");
		}else{

			jsonMap.put("result", false);
			jsonMap.put("message", "好友删除失败!");
		}
		return jsonMap;
	}
	
	@RequestMapping( value="/updatefriend",method=RequestMethod.POST )
	@ResponseBody
	public Map<String,Object> updatefriend(HttpServletRequest request) throws Exception {
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		String friendIdBase64 = request.getParameter("friendId");
		String busername = request.getParameter("friendname");
		String canview = request.getParameter("canview");
		String friendId = SmalljjUtil.decodeBase64(friendIdBase64);
		
		Friend friend = new Friend();
		friend.setId(new BigDecimal(friendId));
		friend.setFriendname(busername);
		friend.setCanview(new BigDecimal(canview));
		
		boolean result = friendService.update( friend );
		if(result){
			jsonMap.put("result", true);
			jsonMap.put("message", "设置好友成功!");
		}else{

			jsonMap.put("result", false);
			jsonMap.put("message", "设置好友失败!");
		}
		return jsonMap;
	}
}